<?php  if (!defined('BASEPATH')) exit('No direct script access allowed');

/**
 * SimplyPost
 *
 * @author		Pascal Kriete
 * @package		SimplyPost
 * @copyright	Copyright (c) 2008, Pascal Kriete
 * @license 	http://www.gnu.org/licenses/lgpl.txt
 */

// ------------------------------------------------------------------------

/**
 * Profile Controller
 *
 * @package		SimplyPost
 * @subpackage	Member
 * @category	Controller
 * @author		Pascal Kriete
 */

class Profile extends Authenticated {

	/**
	 * Controller
	 *
	 * @access	public
	 */
	function Profile()
	{
		parent::Authenticated();
		$this->lang->load('member');		
	}
	
	// ------------------------------------------------------------------------
	
	/**
	 * Redirect to own profile
	 *
	 * @access	public
	 */
	function index()
	{
		$this->session->keep_flashdata('msg');
		redirect('member/profile/view/'.$this->_user['id']);
	}
	
	// ------------------------------------------------------------------------
	
	/**
	 * Show a member profile
	 *
	 * @access	public
	 * @param	integer	member id
	 */
	function view($id = -1)
	{
		
		if ($id == -1)
		{
			redirect('member/profile/');
		}
		
		$this->lang->load('date');
		
		/* Get it from the db */
		$this->load->model('User_model', 'User');
		$userdata = $this->User->get_user($id);		
		
		if ($userdata === FALSE)
		{
			$this->session->set_flashdata('msg', $this->lang->line('user_not_exist'));
			redirect('member/profile/');
		}
		
		set_title($userdata['username']);
		$this->breadcrumb->from_db($id, 'member');

		$userdata['this_user'] = ($userdata['id'] == $this->_user['id']) ? TRUE : FALSE;
		
		$this->parser->parse('member/profile', $userdata);
	}
	
	// ------------------------------------------------------------------------
	
	/**
	 * Show and validate edit profile page for current user
	 *
	 * @access	public
	 */
	function edit()
	{
		set_title($this->lang->line('member_edit_profile_title'));
		$this->breadcrumb->from_path();
		
		$this->load->helper('date');
		$this->load->helper('form');
		$this->load->library('validation');
		
		$this->load->model('User_model', 'User');
		
		$this->validation->set_error_delimiters('<p style="color:#900">', '</p>');
		
		if (isset($_POST['timezones']) )
		{
			$this->User->change(array(
				'timezone' => $this->input->post('timezones')
			));
			$this->session->set_flashdata('msg', $this->lang->line('timezone_changed'));
			redirect('member/profile');
		}
		else if (isset($_POST['old_password']) OR isset($_POST['new_password']) )
		{
			$rules['old_password'] = "trim|required";
			$rules['new_password'] = "trim|required|callback__process";
			$this->validation->set_rules($rules);

			$fields['old_password'] = 'Old Password';
			$fields['new_password'] = 'New Password';
			$this->validation->set_fields($fields);

			$this->validation->set_error_delimiters('<p style="color:#900">', '</p>');

			if ($this->validation->run() == FALSE)
			{
				$this->parser->parse('member/edit_profile', $this->_user);
			}
			else
			{
				$this->User->change(array(
					'password' => $this->input->post('new_password')
				));

				$this->session->set_flashdata('msg', $this->lang->line('password_changed'));
				redirect('member/profile');
			}
		}
		else
		{
			$this->parser->parse('member/edit_profile', $this->_user);
		}
	}
	
	// ------------------------------------------------------------------------
	
	/**
	 * Edit profile validation callback
	 *
	 * @access	private
	 * @param string  new password from $_POST
	 * @return bool   validation pass/fail
	 */
	function _process($new_pw)
	{
		$this->load->library('auth');
		
		$old_pw = $this->input->post('old_password');
		$old_pw = $_POST['old_password'] = secure_password($this->_user['email'], $old_pw);
		$new_pw = $_POST['new_password'] = secure_password($this->_user['email'], $new_pw);
		
		$old_match = $this->auth->try_login(array(
			'username' => $this->_user['username'],
			'password' => $this->input->post('old_password')
		));
		
		if ($old_match)
		{
			return TRUE;
		}
		else
		{
			$this->validation->set_message('_process', 'Authentication Failed.');
			return FALSE;
		}
	}
}